package com.xc.xcrpc.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Mock 服务代理 （jdk动态代理）
 *
 * @author zqx
 * @since 2024/9/23
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {


    /**
     *调用代理
     *
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> methodReturnType = method.getReturnType();
        log.info("mock invoke :{}" ,method.getName());
        return getDefaultObject(methodReturnType);
    }


    /**
     * 生成指定类型的默认类型
     * @param type
     * @return
     */
    private Object getDefaultObject(Class<?> type) {
        //基本类型
        if (type.isPrimitive()){
            if (boolean.class == type){
                return false;
            }else if (short.class == type){
                return (short)0;
            }else if (int.class == type){
                return 0;
            }else if (long.class == type){
                return 0L;
            }
        }
        //对象类型
        return null;
    }


}
